<template>
	<view>
		<div class="list">
			<div class="list-item" v-for="(item, index) in dataList" :key="index" @tap="handleGoods(item.id)">
				<div class="image-wrap">
					<div class="type_name tj">{{item.sign}}</div>
					<image :src="item.cover" mode="scaleToFill" />
				</div>

				<div class="container text-bold">
					<div class="title">{{ item.title }}</div>
					<div class="tags">
						<div v-for="(v, i) in item.service_name" :key="i" class="tags-item">{{ v }}</div>
					</div>
					<div class="retail-price">零售价:
					￥<text class="yuan">{{ price_format("yuan", item.current_price) }}</text>
						<text class="fen">.{{ price_format("fen", item.current_price) }}</text>
						<text>/{{ item.unit_name }}</text>
					</div>
					<div class="price" v-if="item.vip_show>0">
						会员价:￥
						<text class="yuan">{{ price_format("yuan", item.vip_price) }}</text>
						<text class="fen">.{{ price_format("fen", item.vip_price) }}</text>
						<text>/{{ item.unit_name }}</text>
					</div>
				</div>
			</div>
		</div>
	</view>
</template>

<script>
	/**
	 * wanlProduct 商品
	 * @property {Array} dataList 列表数据
	 * @property {String} dataStyle 列表样式
	 * @example <wanl-product />
	 */
	export default {
		name: 'wanlProduct',
		props: {
			dataList: {
				type: Array,
				required: true,
				default: function() {
					return [];
				}
			},
			dataStyle: {
				type: String,
				default: 'col-2-20'
			},
			dataType: {
				type: String,
				default: 'goods'
			}
		},
		data() {
			return {
				addTime: 180,
				leftList: [],
				rightList: [],
				tempList: [],
				children: []
			}
		},
		watch: {
			copyFlowList(nVal, oVal) {
				// 取差值，即这一次数组变化新增的部分
				let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
				if (nVal.slice(startIndex).length == 0) {
					this.leftList = [];
					this.rightList = [];
					this.tempList = this.cloneData(this.copyFlowList);
				} else {
					this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
				}
				this.splitData();
			},
		},
		mounted() {
			this.tempList = this.cloneData(this.copyFlowList);
			this.splitData();
		},
		computed: {
			// 破坏flowList变量的引用，否则watch的结果新旧值是一样的
			copyFlowList() {
				return this.cloneData(this.dataList);
			}
		},
		methods: {
			price_format: function(type, price) {
				var status = (price + '').indexOf('.')
				if (status == '-1') {
					if (type == 'yuan') {
						return price;
					}
					if (type == 'fen') {
						return '00';
					}
				}
				var price_array = (price + '').split('.')
				if (type == 'yuan') {
					return price_array[0];
				}
				if (type == 'fen') {
					var price_fen = price_array[1].length < 2 ? price_array[1] + '0' : price_array[1];
					return price_fen;
				}
			},
			async splitData() {
				if (!this.tempList.length) return;
				let rightRect = await this.getRect('#wanl-right-cloumn');
				let leftRect = await this.getRect('#wanl-left-cloumn');
				// 如果左边小于或等于右边，就添加到左边，否则添加到右边
				let item = this.tempList[0];
				if (!item) return;
				item.cover = this.$wanlshop.oss(item.cover, 172, 0, 1);
				item.comment = this.$wanlshop.toFormat(item.comment, 'hundred');
				item.praise = item.comment > 0 ? parseInt((item.praise / item.comment) * 100) : 0;
				if (leftRect.height < rightRect.height) {
					this.leftList.push(item);
				} else if (leftRect.height > rightRect.height) {
					this.rightList.push(item);
				} else {
					// 这里是为了保证第一和第二张添加时，左右都能有内容
					// 因为添加第一张，实际队列的高度可能还是0，这时需要根据队列元素长度判断下一个该放哪边
					if (this.leftList.length <= this.rightList.length) {
						this.leftList.push(item);
					} else {
						this.rightList.push(item);
					}
				}
				console.log(this.leftList)
				// 移除临时列表的第一项
				this.tempList.splice(0, 1);
				// 如果临时数组还有数据，继续循环
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime)
				}
			},
			// 复制而不是引用对象和数组
			cloneData(data) {
				return JSON.parse(JSON.stringify(data));
			},
			getRect(selector, all) {
				return new Promise(resolve => {
					let query = null;
					// 支付宝小程序不能加后面的.in(this)，是它自身的限制
					// #ifndef MP-ALIPAY
					query = uni.createSelectorQuery().in(this)
					// #endif
					// #ifdef MP-ALIPAY
					query = uni.createSelectorQuery()
					// #endif
					query[all ? 'selectAll' : 'select'](selector)
						.boundingClientRect(rect => {
							if (all && Array.isArray(rect) && rect.length) {
								resolve(rect)
							}
							if (!all && rect) {
								resolve(rect)
							}
						})
						.exec()
				})
			},
			handleGoods(id) {
				if (this.dataType === 'goods') {
					this.onGoods(id);
				} else if (this.dataType === 'groups') {
					this.$wanlshop.to(`/pages/apps/groups/goods?good_id=${id}`);
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.list {
		padding: 10rpx 20rpx;;
		box-sizing: border-box;

		&-item {
			margin-top: 20rpx;
			display: flex;
			background: #fff;
			box-sizing: border-box;
			padding: 20rpx;
			border-radius: 10rpx;

			.image-wrap {
				position: relative;
				overflow: hidden;
				border-radius: 10rpx;
				width: 200rpx;
				height: 200rpx;

				.type_name {
					position: absolute;
					background: #4385ea;
					left: 0;
					bottom: 0;
					padding: 5rpx 15rpx;
					box-sizing: border-box;
					z-index: 111;
					color: #fff;
					font-size: 24rpx;
				}

				image {
					width: 100%;
					height: 100%;
				}
			}

			.container {
				padding-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-around;

				.title {
					font-size: 28rpx;
					color: #2e2e2e;
				}

				.tags {
					display: flex;
					align-items: center;
					font-size: 22rpx;
					margin-top:5px;
					

					&-item {
						background: #eff9ff;
						border-radius: 4rpx;
						border: 1rpx solid #005a93;
						color: #005a93;
						margin-right: 10rpx;
						padding:3rpx;
					}
				}

				.retail-price {
					color: #214a75;
					margin-top:5px;
					font-size: 28rpx;
					.yuan {
						font-size: 34rpx;
					}
					
					.fen {
						font-size: 34rpx;
					}
				}

				.price {
					color: #e90000;
					font-size: 28rpx;

					.yuan {
						font-size: 34rpx;
					}

					.fen {
						font-size: 34rpx;
					}
				}
			}
		}
	}

	// .left_img {
	// 	width: 5rem !important;
	// 	height: 5rem !important;
	// 	margin-right: 0.75rem;
	// 	position: relative;
	// }

	// .left_img img {
	// 	width: 100%;
	// 	border-radius: 0.2rem;
	// }

	// .left_img .sign {
	// 	position: absolute;
	// 	left: 0;
	// 	bottom: 0;
	// 	width: 43px;
	// 	height: 18px;
	// 	background: #4385EA;
	// 	border-radius: 0px 4px 0px 4px;
	// }

	// .left_img .sign .sign_title {
	// 	width: 24px;
	// 	height: 12px;
	// 	font-size: 12px;
	// 	font-family: PingFangSC-Regular, PingFang SC;
	// 	font-weight: 400;
	// 	color: #FFFFFF;
	// 	line-height: 18px;
	// 	text-align: center;
	// 	margin: auto;
	// 	vertical-align: center
	// }

	// .top_title img {
	// 	width: 1.6rem;
	// 	height: 0.9rem;
	// 	display: inline-block;
	// 	transform: translateY(0.15rem);
	// 	margin-right: 0.2rem;
	// }

	// .top_title {
	// 	font-size: 0.7rem;
	// 	/* font-family: PingFangSC-Regular; */
	// 	font-weight: 400;
	// 	color: #222222;
	// 	line-height: 1.1rem;
	// 	max-height: 2.2rem;
	// 	display: inline-block;
	// }

	// .title {
	// 	text-overflow: -o-ellipsis-lastline;
	// 	overflow: hidden;
	// 	text-overflow: ellipsis;
	// 	display: -webkit-box;
	// 	-webkit-line-clamp: 2;
	// 	line-clamp: 2;
	// 	-webkit-box-orient: vertical;
	// }

	// .mid_decri_item {
	// 	padding: 0 0.1rem;
	// 	line-height: 0.75rem;
	// 	border-radius: 0.1rem;
	// 	border: 0.05rem solid #005a93;
	// 	font-size: 0.55rem;
	// 	border-radius: 0.1rem;
	// 	/* font-family: PingFangSC-Regular; */
	// 	font-weight: 400;
	// 	color: #005a93;
	// 	/*text-align: center;*/
	// 	float: left;
	// 	margin-top: 0.3rem;
	// 	margin-right: 0.25rem;
	// 	margin-bottom: 0.4rem;
	// 	padding-top: 0.010rem;
	// 	background: rgba(239, 249, 255, 0.4);
	// }

	.wanl-waterfall {
		display: flex;
		flex-direction: row;
		align-items: flex-start;

		.wanl-cloumn {
			display: flex;
			flex: 1;
			flex-direction: column;
			height: auto;
			width: 50%;

			.wanl-image {
				width: 100%;
			}
		}
	}
</style>